/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metro.jme.lottery.graphics.screens;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Ticker;
import metro.jme.lottery.persist.FavBean;
import metro.jme.lottery.util.ImageHelper;
import metro.jme.lottery.util.StringUtil;

/**
 *
 * @author pierre
 */
public class MainCanvas extends Canvas {

	private static final int TOP_LEFT_ANCHOR = Graphics.TOP | Graphics.LEFT;

	public MainCanvas() {
		System.out.println("Hello Main Canvas");
	}

	/**
	 * Canvas Method
	 * @param grphcs
	 * @see Canvas
	 */
	protected void paint(Graphics grphcs) {
		eraseBackground(grphcs);
		// Add the title image :
		try {
			Image title = Image.createImage(
				ImageHelper.getOptimalImage("/imgs/title.png", getWidth()));
			grphcs.drawImage(title, 0, 0, TOP_LEFT_ANCHOR);
			Image welcome = Image.createImage(
					ImageHelper.getOptimalImage("/imgs/welcome.png", getWidth()));
			int x = getWidth() / 2;
			x = x - welcome.getWidth() / 2;
			grphcs.drawImage(welcome, x, 40, TOP_LEFT_ANCHOR);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Display the favourite !
		// Let's show the favourite set :
		String favSet = FavBean.getFavSet();

		int color_blue = 0x0000ff;

		int font_size = ( getWidth() < ImageHelper.SMALL_DEVICE_TRESHOLD ? Font.SIZE_SMALL : Font.SIZE_MEDIUM);
		
		Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, font_size);
		grphcs.setFont(f);
		grphcs.setColor(color_blue);
		grphcs.drawString("You favourite set is : ", 20, 80, TOP_LEFT_ANCHOR);
		if (StringUtil.isEmpty(favSet)) {
			favSet = "Select a set from the menu";
			f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, font_size);
			grphcs.setFont(f);
			grphcs.setColor(255, 0, 255);
			grphcs.drawString("You haven't created any set", 20, 100, TOP_LEFT_ANCHOR);
		} else {
			grphcs.setColor(color_blue);
		}
		grphcs.drawString(favSet, 20, 120, TOP_LEFT_ANCHOR);
		
		grphcs.setColor(color_blue);
		// Display selected song :
		grphcs.drawString("Selected track : ", 20, 140, TOP_LEFT_ANCHOR);
		grphcs.drawString(TrackSelecter.getCurrentTrackName(), 20, 160, TOP_LEFT_ANCHOR);
		
		this.setTicker(new Ticker("Welcome to the lottery checker, you can compare your favourite numbers with the correct one by using check button"));
	}

	public void eraseBackground(Graphics g) {
		//g.setColor(0x0000ff);
		g.setGrayScale(255);
		g.fillRect(0, 0, getWidth(), getHeight());
	}
}
